package org.jeecg.modules.strategy.component;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.strategy.model.entity.AnalyticsEntity;
import org.jeecg.modules.strategy.util.SpiderUtil;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Component
public class HomePagePageProcessor implements PageProcessor {
    @Override
    public Site getSite() {
        return Site.me()
                .setRetryTimes(1)
                .setSleepTime(1000)
                .setTimeOut(10000)
                .setCharset(StandardCharsets
                        .UTF_8.name());
    }

    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        List<String> metaAll = html.$("head > meta").all();
        if (!metaAll.isEmpty()){
            AnalyticsEntity analyticsEntity = new AnalyticsEntity();
            setAnalyticsEntityToList(metaAll, analyticsEntity);
            setBottom(html, analyticsEntity);
            setPublicInformation(page, analyticsEntity);
        }
    }

    private static void setAnalyticsEntityToList(List<String> metaAll, AnalyticsEntity analyticsEntity) {
        for (String meta : metaAll) {
            String nameTest = null;
            int nameSubstringStart = meta.indexOf("name=\"") + "name=\"".length() ;
            int nameSubstringEnd = meta.indexOf("\"", nameSubstringStart + 1);
            nameTest = meta.substring(nameSubstringStart, nameSubstringEnd);
            if (nameTest.charAt(0) == '“' || nameTest.charAt(0) == '\'')
                nameTest = meta.substring(nameSubstringStart + 1, meta.indexOf("”"));
            String contentTest = new SpiderUtil().getContentTest(meta);
            setAnalyticsEntity(nameTest, analyticsEntity, contentTest);
        }
    }

    private static void setPublicInformation(Page page, AnalyticsEntity analyticsEntity) {
        if (!StringUtils.isEmpty(analyticsEntity.getSiteName())
                        && !StringUtils.isEmpty(analyticsEntity.getSiteDomain())
                        && !StringUtils.isEmpty(analyticsEntity.getSiteIdCode()) ) {
            analyticsEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            analyticsEntity.setUrl(page.getUrl().toString());
            analyticsEntity.setStatus(0);
            analyticsEntity.setCreateTime(date);
            analyticsEntity.setUpdateTime(date);
            analyticsEntity.setIsDelete(0);
            page.putField("analyticsEntity", analyticsEntity);
        }
    }

    private static void setBottom(Html html, AnalyticsEntity analyticsEntity) {
        String text = html.toString();
        if (text.contains("ICP备案") || text.contains("ICP证") || text.contains("备案号")) {
            String pattern = "ICP备案[：:]?\\s*([^<]+?)(?=<|$)";
            String icpFiling = html.regex(pattern).toString();
            analyticsEntity.setIcpFiling(icpFiling);
        }
        if (text.contains("主办单位")) {
            String pattern = "主办单位[:：]?\\s*([^<;，,]+?)(?=[<;，,]|ICP|$)";
            String organizer = html.regex(pattern).toString();
            analyticsEntity.setOrganizer(organizer);
        }
        if (text.contains("承办单位")) {
            String pattern = "承办单位[:：]?\\s*([^<;，,]+?)(?=[<;，,]|ICP|$)";
            String undertaker = html.regex(pattern).toString();
            analyticsEntity.setUndertaker(undertaker);
        }
        if (text.contains("公网备案")) {
            String pattern = "公网备案[:：]?\\s*([^<;，,]+?)(?=[<;，,]|ICP|$)";
            String publicNetworkFiling = html.regex(pattern).toString();
            analyticsEntity.setPublicNetworkFiling(publicNetworkFiling);
        }
    }

    private static void setAnalyticsEntity(String nameTest, AnalyticsEntity analyticsEntity, String contentTest) {
        switch (nameTest) {
            case "SiteName":
            case "siteName":
            case "sitename":
            case "Site_Name":
            case "site_Name":
            case "site_name":
            case "Site-Name":
            case "site-Name":
            case "site-name":
                analyticsEntity.setSiteName(contentTest);
                break;
            case "SiteDomain":
            case "siteDomain":
            case "sitedomain":
            case "siteDoMain":
            case "sitedoMain":
            case "SiteDoMain":
            case "SitedoMain":
            case "Site-Domain":
            case "site-Domain":
            case "site-domain":
            case "site-DoMain":
            case "site-doMain":
            case "Site-DoMain":
            case "Site-doMain":
            case "SiteDo-main":
            case "siteDo-main":
            case "sitedo-main":
            case "siteDo-Main":
            case "sitedo-Main":
            case "SiteDo-Main":
            case "Sitedo-Main":
            case "Site-Do-main":
            case "site-Do-main":
            case "site-do-main":
            case "site-Do-Main":
            case "site-do-Main":
            case "Site-Do-Main":
            case "Site-do-Main":
            case "Site_Domain":
            case "site_Domain":
            case "site_domain":
            case "site_DoMain":
            case "site_doMain":
            case "Site_DoMain":
            case "Site_doMain":
            case "SiteDo_main":
            case "siteDo_main":
            case "sitedo_main":
            case "siteDo_Main":
            case "sitedo_Main":
            case "SiteDo_Main":
            case "Sitedo_Main":
            case "Site_Do_main":
            case "site_Do_main":
            case "site_do_main":
            case "site_Do_Main":
            case "site_do_Main":
            case "Site_Do_Main":
            case "Site_do_Main":
            case "Site-Do_main":
            case "site-Do_main":
            case "site-do_main":
            case "site-Do_Main":
            case "site-do_Main":
            case "Site-Do_Main":
            case "Site-do_Main":
            case "Site_Do-main":
            case "site_Do-main":
            case "site_do-main":
            case "site_Do-Main":
            case "site_do-Main":
            case "Site_Do-Main":
            case "Site_do-Main":
                analyticsEntity.setSiteDomain(contentTest);
                break;
            case "SiteIDCode":
            case "SiteiDCode":
            case "SiteIdCode":
            case "SiteIDcode":
            case "siteIDCode":
            case "siteiDCode":
            case "siteIdCode":
            case "siteIDcode":
            case "siteiDcode":
            case "siteidcode":
            case "Site_IDCode":
            case "Site_iDCode":
            case "Site_IdCode":
            case "Site_IDcode":
            case "site_IDCode":
            case "site_iDCode":
            case "site_IdCode":
            case "site_IDcode":
            case "site_iDcode":
            case "site_idcode":
            case "SiteID_Code":
            case "SiteiD_Code":
            case "SiteId_Code":
            case "SiteID_code":
            case "siteID_Code":
            case "siteiD_Code":
            case "siteId_Code":
            case "siteID_code":
            case "siteiD_code":
            case "siteid_code":
            case "Site_ID_Code":
            case "Site_iD_Code":
            case "Site_Id_Code":
            case "Site_ID_code":
            case "site_ID_Code":
            case "site_iD_Code":
            case "site_Id_Code":
            case "site_ID_code":
            case "site_iD_code":
            case "site_id_code":
            case "Site-IDCode":
            case "Site-iDCode":
            case "Site-IdCode":
            case "Site-IDcode":
            case "site-IDCode":
            case "site-iDCode":
            case "site-IdCode":
            case "site-IDcode":
            case "site-iDcode":
            case "site-idcode":
            case "SiteID-Code":
            case "SiteiD-Code":
            case "SiteId-Code":
            case "SiteID-code":
            case "siteID-Code":
            case "siteiD-Code":
            case "siteId-Code":
            case "siteID-code":
            case "siteiD-code":
            case "siteid-code":
            case "Site-ID_Code":
            case "Site-iD_Code":
            case "Site-Id_Code":
            case "Site-ID_code":
            case "site-ID-Code":
            case "site-iD-Code":
            case "site-Id-Code":
            case "site-ID-code":
            case "site-iD-code":
            case "site-id-code":
            case "Site-ID-Code":
            case "Site-iD-Code":
            case "Site-Id-Code":
            case "Site-ID-code":
            case "site-ID_Code":
            case "site-iD_Code":
            case "site-Id_Code":
            case "site-ID_code":
            case "site-iD_code":
            case "site-id_code":
            case "Site_ID-Code":
            case "Site_iD-Code":
            case "Site_Id-Code":
            case "Site_ID-code":
            case "site_ID-Code":
            case "site_iD-Code":
            case "site_Id-Code":
            case "site_ID-code":
            case "site_iD-code":
            case "site_id-code":
                analyticsEntity.setSiteIdCode(contentTest);
                break;
        }
    }
}
